package com.burgeon.backend.EnterpriseWeChat.rest;

import com.burgeon.backend.EnterpriseWeChat.entity.Retail;
import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetRateItemByEmpId extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public GetRateItemByEmpId(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<Retail> getData(String empId, String startDate, String endTime, int page,String token) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(empId,startDate,endTime,page), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Retail> retails = new ArrayList<>();

        if(jsonArray.length()>0){
            for (int i = 0; i < jsonArray.length(); i++) {
                Retail retail = new Retail();
                retail.setStoreName(jsonArray.getJSONArray(i).getString(0));
                retail.setBillDate(jsonArray.getJSONArray(i).getString(1));
                retail.setProductId(jsonArray.getJSONArray(i).getString(2));
                retail.setRetailDocNo(jsonArray.getJSONArray(i).getString(3));
                retail.setProductValue(jsonArray.getJSONArray(i).getString(4));
                retail.setProductName(jsonArray.getJSONArray(i).getString(5));
                retail.setProductColor(jsonArray.getJSONArray(i).getString(6));
                if(jsonArray.getJSONArray(i).getString(7).equals("null")){
                    retail.setProductSize("-");
                }else {
                    retail.setProductSize(jsonArray.getJSONArray(i).getString(7));
                }
                if(jsonArray.getJSONArray(i).getString(8).equals("null")){
                    retail.setProductSize("-");
                }else {
                    retail.setTotAmtActual(jsonArray.getJSONArray(i).getString(8));
                }

                if("null".equals(jsonArray.getJSONArray(i).getString(9))){
                    retail.setSalesRepsRate("无");
                }else {
                    retail.setSalesRepsRate(jsonArray.getJSONArray(i).getString(9));
                }
                retail.setType(token);
                retails.add(retail);
                System.err.println(retail);
            }
        }
        return retails;
    }

    private MultiValueMap Data(String empId,String startDate,String endDate,int page) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("'M_RETAIL_ID;C_STORE_ID;NAME'");//店铺名
        colums.add("'M_RETAIL_ID;BILLDATE'");//日期
        colums.add("'M_RETAILITEM_ID;M_PRODUCT_ID'");//id
        colums.add("'M_RETAIL_ID;DOCNO'");//单据编号
        colums.add("'M_RETAILITEM_ID;M_PRODUCT_ID;VALUE'");//品名
        colums.add("'M_RETAILITEM_ID;M_PRODUCT_ID;NAME'");//款号
        colums.add("'M_PRODUCTALIAS_ID;M_ATTRIBUTESETINSTANCE_ID;VALUE1'");//颜色
        colums.add("'M_PRODUCTALIAS_ID;M_ATTRIBUTESETINSTANCE_ID;VALUE2_CODE'");//尺寸
        colums.add("'M_RETAILITEM_ID;TOT_AMT_ACTUAL'");//成交价
        colums.add("'M_RETAILITEM_ID;SALESREPS_RATE'");//比例


//        colums.add("'M_RETAIL_ID;C_STORE_ID;NAME'");//店铺名
//        colums.add("'M_RETAIL_ID;BILLDATE'");//日期
//        colums.add("'M_PRODUCT_ID'");//id
//        colums.add("'M_RETAIL_ID;DOCNO'");//单据编号
//        colums.add("'M_PRODUCT_ID;VALUE'");//品名
//        colums.add("'M_PRODUCT_ID;NAME'");//款号
//        colums.add("'M_PRODUCTALIAS_ID;M_ATTRIBUTESETINSTANCE_ID;VALUE1'");//颜色
//        colums.add("'M_PRODUCTALIAS_ID;M_ATTRIBUTESETINSTANCE_ID;VALUE2_CODE'");//尺寸
//        colums.add("'TOT_AMT_ACTUAL'");//成交价
//        colums.add("'M_RETAIL_ID;AVG_DISCOUNTSJ'");//折扣

        params.put("columns", colums);
        params.put("table", "M_RETAIL_RATEITEM");//表名
        params.put("range",RANGE);
//        params.put("count","true");
        int r = Integer.valueOf(RANGE);
        params.put("start",String.valueOf((page-1)*r));

        HashMap orderby = new HashMap();
        orderby.put("column","'M_RETAIL_ID;DOCNO'");
        orderby.put("asc","false");
        HashMap orderby1 = new HashMap();
        orderby1.put("column","'M_RETAIL_ID;BILLDATE'");
        orderby1.put("asc","false");
        ArrayList list = new ArrayList();
        list.add(orderby);
        list.add(orderby1);
        params.put("orderby",list);

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();

        expr1.put("column", "'SALESREP_ID'");//M_RETAIL_ID;C_STORE_ID
        expr1.put("condition", empId);



        HashMap expr2 = new HashMap();
        String ac =  "'"+startDate+"~"+endDate+"'";
        expr2.put("column", "'M_RETAIL_ID;BILLDATE'");
        expr2.put("condition", ac);

        paramss.put("combine", "and");
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetRateItemByEmpId g = new GetRateItemByEmpId("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("5138","202101"+"01","20210104",1,""));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
